package com.eachonline.smart.service.imp;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.eachonline.mapper.GenericMapper;
import com.eachonline.service.GenericServiceImpl;
import com.eachonline.smart.mapper.SmartOrderRecordMapper;
import com.eachonline.smart.model.SmartCourse;
import com.eachonline.smart.model.SmartOrderRecord;
import com.eachonline.smart.model.SmartOrderRecordExample;
import com.eachonline.smart.model.SmartUserCourse;
import com.eachonline.smart.service.ClassTeacherService;
import com.eachonline.smart.service.CourseService;
import com.eachonline.smart.service.OrderRecordService;
import com.eachonline.smart.service.UserCourseService;
import com.eachonline.smart.util.OrderStatus;
import com.eachonline.util.StringUtil;
import com.eachonline.util.UUIDUtil;
@Service
public class OrderRecordServiceImp extends GenericServiceImpl<SmartOrderRecord, SmartOrderRecordExample, String> implements OrderRecordService {

	@Resource
	public SmartOrderRecordMapper orderRecordMapper;
	
	@Resource
	public UserCourseService userCourseService ;
	
	@Resource
	public CourseService courseService ;
	
	
	@Resource
	public ClassTeacherService classTeacherService ;
	
	@Override
	protected GenericMapper<SmartOrderRecord, SmartOrderRecordExample, String> getGenericMapper() {
		// TODO Auto-generated method stub
		return this.orderRecordMapper;
	}

	/**
	 * 验证是否主订单号
	 * @param orderNo
	 */
	@Override
	public boolean verifyIsMainTradeNo(String orderNo) {
		// TODO Auto-generated method stub
		String prifStr=orderNo.substring(0,2);
		return "MN".equals(prifStr);
	}

	
	
	@Override
	public void updateOrderStatusAndStudy(String orderNo) throws Exception {
		// TODO Auto-generated method stub
		if(verifyIsMainTradeNo(orderNo))
		{
			SmartOrderRecordExample ore=new SmartOrderRecordExample();
			ore.createCriteria().andMainOrderNoEqualTo(orderNo).andOrderStatusEqualTo(OrderStatus.WAIT);
			List<SmartOrderRecord> orderRcordList=getGenericMapper().selectByExample(ore);
			  if(orderRcordList.isEmpty())
			  {
			      return ;
			  }
			  for(SmartOrderRecord order:orderRcordList)
			  {
				  order.setOrderStatus(OrderStatus.SUCCESS);
				  getGenericMapper().updateByPrimaryKeySelective(order);
				  addMyCourse(order.getBusinessId(),order.getUserId());
				  classTeacherService.joinClass(order.getBusinessId(),order.getUserId());
			  }
			
		}else{
			  SmartOrderRecordExample orderExample=new SmartOrderRecordExample();
			  orderExample.createCriteria().andOrderNoEqualTo(orderNo).andOrderStatusEqualTo(OrderStatus.WAIT);
			  List<SmartOrderRecord> orderRcordList=getGenericMapper().selectByExample(orderExample);
			  if(orderRcordList.isEmpty())
			  {
			      return ;
			  }
			  SmartOrderRecord  ordeRecord=orderRcordList.get(0);
			  ordeRecord.setOrderStatus(OrderStatus.SUCCESS);
			  getGenericMapper().updateByPrimaryKeySelective(ordeRecord);
			  addMyCourse(ordeRecord.getBusinessId(),ordeRecord.getUserId());
			  classTeacherService.joinClass(ordeRecord.getBusinessId(),ordeRecord.getUserId());
			}
		}
	  
	public void  addStudyCourseNum(String bussinessId)
	{
		SmartCourse course=courseService.selectByPrimaryKey(bussinessId);
		String studyNum=course.getStudyNum();
		if(StringUtil.isEmpty(studyNum))
		{
			studyNum="0";
		}
		int num=0;
		try {
			  num=Integer.valueOf(studyNum)+1;
		} catch (Exception e) {
			// TODO: handle exception
			  num=1;
		}
	   
	    course.setStudyNum(num+"");
	    courseService.updateByPrimaryKeySelective(course);
	    
	}

    /**
     * 把课程添加到我的课程中
     * @param BusinessId
     */
	public void addMyCourse(String BusinessId,String userId)
	{
		SmartUserCourse  userCourse=new SmartUserCourse();
		userCourse.setSid(UUIDUtil.getUUID());
		userCourse.setCreateTime(new Date());
		userCourse.setCourseId(BusinessId);
		userCourse.setUserId(userId);
		userCourseService.insert(userCourse);
		addStudyCourseNum(BusinessId);
	}

	@Override
	public void updateOrderStatusOnFail(String orderNo) throws Exception {
		if(verifyIsMainTradeNo(orderNo))
		{
			
		}else{
			
			  SmartOrderRecordExample orderExample=new SmartOrderRecordExample();
			  orderExample.createCriteria().andOrderNoEqualTo(orderNo).andOrderStatusEqualTo(OrderStatus.WAIT);
			  List<SmartOrderRecord> orderRcordList=getGenericMapper().selectByExample(orderExample);
			  if(orderRcordList==null)
			  {
			      return ;
			  }
			  SmartOrderRecord  ordeRecord=orderRcordList.get(0);
			  ordeRecord.setOrderStatus(OrderStatus.FAIL);
			  getGenericMapper().updateByPrimaryKeySelective(ordeRecord);
			
			
		}
		
	}
	
	
}